
package botlab.control;


import botlab.Game;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;


/**
 *
 * @author Quentin
 */
public class CustomKeyControl extends BaseControl implements AnalogListener, ActionListener
{
    private Vector3f straight,left,up;
    
    public CustomKeyControl()
    {
        //super(); // executé par défaut !
        initMapping();
        straight=Game.getApp().getCamera().getDirection();
        left=Game.getApp().getCamera().getLeft();
        up=Game.getApp().getCamera().getUp();

    }
    
    private void initMapping()
    {
        inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_RIGHT));
        inputManager.addMapping("forward", new KeyTrigger(KeyInput.KEY_UP));
        inputManager.addMapping("backward", new KeyTrigger(KeyInput.KEY_DOWN));
        inputManager.addMapping("up", new KeyTrigger(KeyInput.KEY_Z));
        inputManager.addMapping("down", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addListener(this, new String[]{"left", "right","forward","backward","up","down"});
    }
    

    public Control cloneForSpatial(Spatial spatial) 
    {
        CustomKeyControl control = new CustomKeyControl();
        // set custom properties
        control.setSpatial(spatial);
        control.setEnabled(isEnabled()); 
        // set some more properties here...
        return control;
    }
 
    @Override
    protected void controlUpdate(float tpf)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * This is is the method that is called when you press a key.
     * It only execute analogic tasks, whereas onAction exectute a discret task
     * reimplemented from AnalogListener
     */
    @Override
    public void onAnalog(String name, float value, float tpf) 
    {
        straight=Game.getApp().getCamera().getDirection();
        left=Game.getApp().getCamera().getLeft();
        up=Game.getApp().getCamera().getUp();
        
        if (name.equals("forward"))
        {
            spatial.move(straight.mult(10*tpf));
        }
        else if (name.equals("backward"))
        {
            spatial.move(straight.mult(-10*tpf));
        }
        else if (name.equals("up"))
        {
            spatial.move(up.mult(10*tpf));
        }
        else if (name.equals("down"))
        {
            spatial.move(up.mult(-10*tpf));
        }
        else if (name.equals("right"))
        {
            spatial.move(left.mult(-10*tpf));
        }
        else if (name.equals("left"))
        {
            spatial.move(left.mult(10*tpf));
        }
    }
    
    @Override
    public void onAction(String name, boolean value, float tpf)
    {
        
    }
    
    
}
